package com.xtjc.recsyslog.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;

/**
 * 进制转换
 *
 * @author hasee
 */
@Slf4j
public class ConverUtil {


    /**
     * 将16进制字符串转换为字符串数组
     *
     * @param hexString 16进制字符串
     * @return 字符串数组
     */
    public static String[] convertHexStringToStringArray(String hexString) {
        int length = hexString.length();
        int arrayLength = length / 2;

        String[] hexArray = new String[arrayLength];

        for (int i = 0; i < length; i += 2) {
            hexArray[i / 2] = hexString.substring(i, i + 2);
        }

        return hexArray;
    }

    public static byte[] hexArrayToByteArray(String[] hexStrings) {
        int i = 0;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        for (String hexString : hexStrings) {
            if (StrUtil.isEmpty(hexString)) {
                log.error("第{}个字节为空", i);
            }
            i++;
            int intValue = Integer.parseInt(hexString, 16);
            byteArrayOutputStream.write(intValue); // 写入 byte 值
        }
        return byteArrayOutputStream.toByteArray(); // 转换为 byte 数组并返回
    }

    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(0xFF & b);
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex.toUpperCase(); // 可选：如果需要大写的 16 进制字符
    }

    public static String byteToHex(int b) {
        String hex = Integer.toHexString(b);
        if (hex.length() == 1 || hex.length() == 3) {
            hex = "0" + hex;
        }
        return hex.toUpperCase(); // 可选：如果需要大写的 16 进制字符
    }

    public static String[] bytesToHexArray(byte[] bytes) {
        String[] hexStrings = new String[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]).toUpperCase();
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            hexStrings[i] = hex;
        }
        return hexStrings;
    }

    /**
     * 字符串转换为Ascii
     *
     * @param value
     * @return
     */
    public static String stringTransformAscii(String value) {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]).append(",");
            } else {
                sbu.append((int) chars[i]);
            }
        }
        return sbu.toString();
    }

    public static String[] stringTransformAsciiToArray(String value) {
        // 获取输入字符串的长度
        int length = value.length();

        // 初始化结果数组，长度与输入字符串相同
        String[] asciiValues = new String[length];

        // 遍历输入字符串中的每个字符
        char[] chars = value.toCharArray();
        for (int i = 0; i < length; i++) {
            // 将当前字符的 ASCII 值转换为字符串，并存储在结果数组中
            asciiValues[i] = String.valueOf((int) chars[i]);
        }

        return asciiValues;
    }


    /**
     * Ascii转换为字符串
     *
     * @param value
     * @return
     */
    public static String asciiTransformString(String value) {
        StringBuffer sbu = new StringBuffer();
        String[] chars = value.split(",");
        for (int i = 0; i < chars.length; i++) {
            sbu.append((char) Integer.parseInt(chars[i]));
        }
        return sbu.toString();
    }
}